/*
 * dataset.h
 *
 *  Created on: Apr 30, 2011
 *      Author: tqlong
 */

#ifndef DATASET_H_
#define DATASET_H_

#include <vector>
#include <armadillo>
#include <fstream>

class InMemoryDataset
{
public:
  typedef arma::vec          vec_type;
  typedef std::vector<vec_type>               vvec_type;
protected:
  std::string m_fName;
  std::string m_format;
  bool m_inMem;
  vvec_type m_set;
  vec_type m_vec;
  long long m_n, m_dim;

  long long m_idx, m_entrySize, m_offset;
  std::ifstream m_input;

  int load(const std::string& fName, const std::string& format, bool inMem);
  /** compute m_n, m_dim, m_offset, m_entrySize */
  virtual void computeEntrySize();
  /** read a vector at current position */
  virtual void readEntry();
  /** seek to position of idx-th vector */
  void seek(int idx);

public:
  InMemoryDataset();
  InMemoryDataset(const std::string& fName, const std::string& format, bool inMem = true);

  /** prepare the dataset to read data */
  void reload();

  // we do not use const functions here to provide more flexibility
  // when a dataset may comes from a very large file
  int n();
  int n_dim();
  const vec_type& get(int  i);     // get vector
  double get(int i, int dim);  // get vector element
};

class RaDecDataset : public InMemoryDataset
{
public:
	RaDecDataset(const std::string& fName) : InMemoryDataset(fName, std::string("raw"), false) {}
	/** compute m_n, m_dim, m_offset, m_entrySize */
  virtual void computeEntrySize();
  /** read a vector at current position */
  virtual void readEntry();
};

#endif /* DATASET_H_ */
